package com.wbcs.system.assistant;

import java.util.HashMap;
import java.util.Map;

import com.wbcs.system.permission.permissiontype.AbsPermissionType;
import com.wbcs.system.permission.permissiontype.DisabledPermissionType;
import com.wbcs.system.permission.permissiontype.DisplayPermissionType;
import com.wbcs.system.permission.permissiontype.ReadonlyPermissionType;

public class AuthorizationAssistant
{
    private final static AuthorizationAssistant instance=new AuthorizationAssistant();
    
    private Map<String,AbsPermissionType> mPermissionTypes;
    
    private AuthorizationAssistant()
    {
        mPermissionTypes=new HashMap<String,AbsPermissionType>();
        AbsPermissionType typeObj=new ReadonlyPermissionType();
        mPermissionTypes.put(typeObj.getTypeName(),typeObj);
        typeObj=new DisabledPermissionType();
        mPermissionTypes.put(typeObj.getTypeName(),typeObj);
        typeObj=new DisplayPermissionType();
        mPermissionTypes.put(typeObj.getTypeName(),typeObj);
    }
    
    public static AuthorizationAssistant getInstance()
    {
        return instance;
    }
    
    public boolean isExistPermissiontype(String permissiontype)
    {
        if(permissiontype==null||permissiontype.trim().equals("")) return false;
        return mPermissionTypes.containsKey(permissiontype.trim());
    }
    
    public String getPermissionTypeDefaultvalue(String permissiontype)
    {
        if(!isExistPermissiontype(permissiontype)) return null;
        return mPermissionTypes.get(permissiontype).getDefaultvalue();
    }
    
    public boolean checkDefaultPermissionTypeValue(String permissiontype)
    {
        return checkDefaultPermissionTypeValue(permissiontype,"true");
    }
    
    public boolean checkDefaultPermissionTypeValue(String permissiontype,String permissionvalue)
    {
        if(!isExistPermissiontype(permissiontype)) return false;
        if(!isExistValueOfPermissiontype(permissiontype,permissionvalue)) return false;
        return mPermissionTypes.get(permissiontype).checkDefaultPermissionValue(permissionvalue);
    }
    
    public boolean isExistValueOfPermissiontype(String permissiontype,String permissionvalue)
    {
        if(permissionvalue==null||permissionvalue.trim().equals("")) return false;
        if(!isExistPermissiontype(permissiontype)) return false;
        permissionvalue=permissionvalue.toLowerCase().trim();
        return mPermissionTypes.get(permissiontype).getLstPermissionValues().contains(permissionvalue);
    }
    
    public boolean isConsistentWithParentPermission(String permissiontype,String permissionvalue,boolean parentPermission)
    {
        if(!isExistPermissiontype(permissiontype)) return false;
        return mPermissionTypes.get(permissiontype).isConsistentWithParentPermission(permissionvalue,parentPermission);
    }
}

